﻿Imports System.IO.File
Imports TwitterVB2

Public Class frmAutoTweet

    Public lastTweet As String
    Public tweetDate As New ListBox
    Dim tw As New TwitterAPI
    Dim tmrMinutes As Integer = 0
    Private IsFormBeingDragged As Boolean = False
    Private MousePos As New System.Drawing.Point(0, 0)

    Private Sub txtBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBox.TextChanged

        On Error Resume Next

        lblCount.Text = Len(txtBox.Text)

        If Len(txtBox.Text) > 100 Then

            lblCount.ForeColor = Color.Orange

        ElseIf Len(txtBox.Text) > 120 Then

            lblCount.ForeColor = Color.OrangeRed

        ElseIf Len(txtBox.Text) = 140 Then

            lblCount.ForeColor = Color.Red

        ElseIf Len(txtBox.Text) <= 99 Then

            lblCount.ForeColor = Color.Green

        End If

    End Sub

    Private Sub btnAdd_MouseEnter(sender As Object, e As System.EventArgs) Handles btnAdd.MouseEnter

        On Error Resume Next

        btnAdd.BackColor = Color.DimGray
        btnAdd.ForeColor = Color.Blue

    End Sub

    Private Sub btnAdd_MouseLeave(sender As Object, e As System.EventArgs) Handles btnAdd.MouseLeave

        On Error Resume Next

        btnAdd.BackColor = Color.Black
        btnAdd.ForeColor = Color.White

    End Sub

    Private Sub frmAutoTweet_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

        Try


            Dim thePath As String = Application.StartupPath & "\" & tw.AccountInformation.ScreenName.ToLower & "_ads.dat"
            If ListView1.Items.Count > 0 Then
                Dim myWriter As New IO.StreamWriter(thePath, False)
                For Each myItem As ListViewItem In ListView1.Items
                    With myItem
                        myWriter.WriteLine(myItem.Text & "|_SP_|" & myItem.SubItems(1).Text)
                        '& "|" & myItem.SubItems(2).Text
                    End With

                Next
                myWriter.Close()


            End If

        Catch ex As Exception

        End Try
        
    End Sub

    Private Sub frmAutoTweet_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If e.CloseReason = CloseReason.UserClosing Then

            e.Cancel = True
            Me.Visible = False

            'ShowInTaskbar = False
            'Me.WindowState = FormWindowState.Minimized

        End If

    End Sub

    Private Sub frmAutoTweet_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        On Error Resume Next

        TabControl1.SelectedTab = TabPage5

        lastTweet = 0

        lblMin.Parent = PictureBox2
        lblMax.Parent = PictureBox2
        lblExit.Parent = PictureBox2

        lblMin.BackColor = Color.Transparent
        lblMax.BackColor = Color.Transparent
        lblExit.BackColor = Color.Transparent

        ToolTip1.SetToolTip(txtUses, "Use 0 for infinite times")

        Dim CurrentTime = Split(DateAndTime.Now.ToString, " ")

        Dim theDate, theTime, theMeridiem

        theDate = CurrentTime(0)
        theTime = Split(CurrentTime(1), ":")
        theMeridiem = CurrentTime(2)

        txtDate_1.Text = theTime(0)
        txtDate_2.Text = theTime(1)
        txtDate_3.Text = theMeridiem

        DateTimePicker1.Value = theDate

        chkAutoTweet.Checked = My.Settings.chkAutoTweet
        chkAddCD.Checked = My.Settings.chkAddCD
        chkOverrideCD.Checked = My.Settings.chkOverrideCD

    End Sub

    Private Sub txtInterval_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtInterval.KeyDown

        On Error Resume Next

        If e.KeyCode = Keys.Enter Then

            If txtInterval.Text > 0 And (txtInterval.Text * 60000) <= 399999999 Then

                tmrRandom.Stop()
                tmrRandom.Interval = txtInterval.Text * 60000
                tmrRandom.Start()

            Else

                MsgBox("Interval is too long!")

            End If

        End If

    End Sub

    Private Sub chkAutoTweet_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkAutoTweet.CheckedChanged

        My.Settings.chkAutoTweet = chkAutoTweet.Checked

        My.Settings.Save()

        If chkAutoTweet.Checked = True Then

            Timer2.Enabled = True
            Timer2.Start()

        Else

            Timer2.Enabled = False
            Timer2.Stop()

        End If

    End Sub

    Private Sub btnTweet_Click(sender As System.Object, e As System.EventArgs) Handles btnTweet.Click

        tw.AuthenticateWith(frmMain.consumer, frmMain.consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)

        If chkRandom.Checked = True Then

            Dim randomTweet As New Random

reRandom:

            Dim where = randomTweet.Next(0, ListView1.Items.Count - 1)

            If ListView1.Items.Item(where).Checked = True And ListView1.SelectedItems.Item(0).Text <> lastTweet Then

                tw.Update(ListView1.Items.Item(where).Text)

            Else : GoTo reRandom

            End If

            lastTweet = ListView1.Items.Item(where).Text

        End If

        frmMain.lblTotalTweets.Text = tw.AccountInformation.StatusesCount

    End Sub

    Private Sub PictureBox2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown

        If e.Button = MouseButtons.Left Then

            IsFormBeingDragged = True

            MousePos = e.Location

        End If


    End Sub

    Private Sub PictureBox2_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove

        If IsFormBeingDragged Then

            Dim temp As Point = New Point(Me.Location + (e.Location - MousePos))
            Me.Location = temp
            temp = Nothing

        End If

    End Sub

    Private Sub PictureBox2_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp

        If e.Button = MouseButtons.Left Then IsFormBeingDragged = False

    End Sub

    Private Sub lblMin_Click(sender As System.Object, e As System.EventArgs) Handles lblMin.Click

        On Error Resume Next

        'If Me.WindowState = FormWindowState.Minimized Then

        Me.Visible = False

        'End If

    End Sub

    Private Sub lblExit_Click(sender As System.Object, e As System.EventArgs) Handles lblExit.Click
        
        Me.Visible = False

    End Sub

    Private Sub btnAdd_Click_1(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click

        If (Len(txtBox.Text) > 0) Then

            ListView1.BeginUpdate()
            Dim n As New ListViewItem
            n = ListView1.Items.Add(txtBox.Text)
            n.Checked = True

            If chkDaily.Checked = True Then

                n.SubItems.Add(txtDate_1.Text + ":" + txtDate_2.Text + " " + txtDate_3.Text + " - Daily")

            Else

                n.SubItems.Add(txtDate_1.Text + ":" + txtDate_2.Text + " " + txtDate_3.Text + " - " + DateTimePicker1.Value)

            End If

            n.SubItems.Add("0")

            ListView1.Refresh()
            ListView1.EndUpdate()

            '  ListView1.Items.Add(txtBox.Text)

            txtBox.Text = ""

        End If

    End Sub

    Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs) Handles Timer2.Tick

        On Error Resume Next

        'btnTweet.PerformClick()
        '----IMPORTANT!
        'HAVE TO CODE ENTIRE NEW TWEET SYSTEM, complete with date, time, am/pm, etc. Better scheduling! BIG BUX HERE.

        Dim i As Integer

        Dim CurrentTime = Split(DateAndTime.Now.ToString, " ")

        Dim theDate, theTime, theMeridiem, theFixedTime

        theDate = CurrentTime(0)
        theTime = Split(CurrentTime(1), ":")
        theMeridiem = CurrentTime(2)

        theFixedTime = theTime(0) + ":" + theTime(1)

        'MsgBox(theFixedTime & " " & theMeridiem & " - " & theDate & " -- " & lstAT_Time.Items.Item(0))

        'sort out time and date

        Dim theRealTime = theFixedTime & " " & theMeridiem & " - " & theDate
        Dim theREELTime = theFixedTime & " " & theMeridiem & " - Daily"

        'a dated tweet

        For Each myItem As ListViewItem In ListView1.Items

            If myItem.SubItems(1).Text = theRealTime Then

                Dim where = ListView1.Items.IndexOf(theRealTime)

                If ListView1.Items.Item(where).Checked = True Then

                    If ListView1.Items.Item(where).SubItems(2).Text = 0 Or chkOverrideCD.Checked = True Then

                        tw.AuthenticateWith(frmMain.consumer, frmMain.consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)
                        tw.Update(ListView1.Items.Item(where).Text)

                        GroupBox3.Text = "Last sent tweet: (" & TimeOfDay & ")"

                        lblLastTweet.Text = frmMain.GroupBox2.Text & ": " & ListView1.Items.Item(where).Text

                        If chkAddCD.Checked = True Then

                            ListView1.Items.Item(where).SubItems(2).Text = 30

                        End If

                        'If chkDeleteTweets.Checked = True Then

                        'ListView1.Items.RemoveAt(where)
                        'lstAT_Time.Items.RemoveAt(where)
                        'lstAT_Uses.Items.RemoveAt(where)

                        'Else

                        ListView1.Items.Item(where).Checked = False

                        'End If

                    End If

                End If

            End If

        Next

        'a daily tweet
        For Each myItem As ListViewItem In ListView1.Items

            If myItem.SubItems(1).Text = theREELTime Then

                Dim where = ListView1.Items.IndexOf(theREELTime)

                If ListView1.Items.Item(where).Checked = CheckState.Checked Then

                    tw.AuthenticateWith(frmMain.consumer, frmMain.consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)
                    tw.Update(ListView1.Items.Item(where).Text)

                    GroupBox3.Text = "Last sent tweet: (" & TimeOfDay & ")"

                    lblLastTweet.Text = frmMain.GroupBox2.Text & ": " & ListView1.Items.Item(where).Text

                    If chkAddCD.Checked = True Then

                        ListView1.Items.Item(where).SubItems(2).Text = 30

                    End If

                End If

            End If

        Next

    End Sub

    Private Sub chkDaily_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkDaily.CheckedChanged

        If chkDaily.CheckState = CheckState.Checked Then

            DateTimePicker1.Enabled = False

        Else

            DateTimePicker1.Enabled = True

        End If

    End Sub

    Private Sub chkRandomTweet_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkRandomTweet.CheckedChanged

        If chkRandomTweet.Checked = True Then

            tmrRandom.Enabled = True

        Else

            tmrRandom.Enabled = False
            tmrRandom.Stop()

        End If

    End Sub

    Private Sub tmrRandom_Tick(sender As System.Object, e As System.EventArgs) Handles tmrRandom.Tick

        On Error Resume Next

        Dim randomTweet As New Random

        'If tmrMinutes > 0 Then

        'tmrMinutes -= 1

        'Else

        tw.AuthenticateWith(frmMain.consumer, frmMain.consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)

reRandom:

        Dim theTweet = randomTweet.Next(0, ListView1.Items.Count - 1)

        If ListView1.Items.Item(theTweet).Checked = True And ListView1.Items.Item(theTweet).SubItems(2).Text = 0 Or _
            ListView1.Items.Item(theTweet).Checked = True And chkAddCD.Checked = False Then

            tw.Update(ListView1.Items.Item(theTweet).Text)

            GroupBox3.Text = "Last sent tweet: (" & TimeOfDay & ")"

            lblLastTweet.Text = frmMain.GroupBox2.Text & ": " & ListView1.Items.Item(theTweet).Text

            If chkAddCD.Checked = True Then

                ListView1.Items.Item(theTweet).SubItems(2).Text = 30

            End If

        Else : GoTo reRandom

        End If

        tmrMinutes = txtInterval.Text

        'End If

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        On Error Resume Next

        Dim i As Integer

        For i = 0 To ListView1.Items.Count - 1

            'lstAutoTweet.SetItemChecked(i, False)
            ListView1.Items.Item(i).Checked = False

        Next

    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

        'On Error Resume Next

        MsgBox(ListView1.Items.Item(0).SubItems.Item(1).Text)
        TextBox1.Text = TextBox1.Text + 1

    End Sub

    Private Sub mnuEditDate_Click(sender As System.Object, e As System.EventArgs)


    End Sub

    Private Sub EditTweetToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EditTweetToolStripMenuItem.Click

        On Error Resume Next

        Dim newTime = InputBox("Change this tweet:", "Change this Tweet", ListView1.SelectedItems.Item(0).Text)

        If newTime.Length > 0 And newTime IsNot "" Then

            ListView1.SelectedItems.Item(0).Text = newTime.ToString

        End If

    End Sub

    Private Sub EditDateOrTimeToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EditDateOrTimeToolStripMenuItem.Click

        Dim theTime As ListViewItem = ListView1.SelectedItems.Item(0)

        Dim newTime = InputBox("Edit Time" & vbNewLine _
                               & vbNewLine & "Example: 1:52 AM - 6/17/2012" _
                               & vbNewLine _
                               & "or 1:52 AM - Daily", "Edit Date/Time", theTime.SubItems(1).Text.ToString)

        If newTime.Length > 0 And newTime IsNot "" Then

            theTime.SubItems(1).Text = newTime.ToString

        End If

    End Sub

    Private Sub DeleteTweetToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DeleteTweetToolStripMenuItem.Click

        If MsgBox("Delete this tweet?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then

            Dim where = ListView1.SelectedIndices(0)

            ListView1.Items.RemoveAt(where)

        End If

    End Sub

    Private Sub tmrCooldown_Tick(sender As System.Object, e As System.EventArgs) Handles tmrCooldown.Tick

        If ListView1.Items.Count > 0 Then

            For Each myItem As ListViewItem In ListView1.Items

                With myItem

                    If myItem.SubItems(2).Text > 0 Then

                        myItem.SubItems(2).Text = myItem.SubItems(2).Text - 1

                    End If

                End With

            Next

        End If

    End Sub

    Private Sub chkAddCD_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkAddCD.CheckedChanged

        If chkAddCD.Checked = True Then

            'tmrCooldown.Enabled = True
            '     ListView1.Columns.Item(0).Width = 252

        Else

            'tmrCooldown.Enabled = False
            'tmrCooldown.Stop()
            '     ListView1.Columns.Item(0).Width = 307

        End If

        My.Settings.chkAddCD = chkAddCD.Checked
        My.Settings.Save()

    End Sub

    Private Sub chkOverrideCD_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkOverrideCD.CheckedChanged

        My.Settings.chkOverrideCD = chkOverrideCD.Checked
        My.Settings.Save()

    End Sub

    Private Sub chkDeleteTweets_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkDeleteTweets.CheckedChanged

        My.Settings.chkDeleteTweets = chkDeleteTweets.Checked
        My.Settings.Save()

    End Sub

    Private Sub CustomCooldownToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CustomCooldownToolStripMenuItem.Click

        Dim theCD As ListViewItem = ListView1.SelectedItems.Item(0)

        Dim newCD = InputBox("Make a custom cooldown period: (in minutes)", "Custom Cooldown", theCD.SubItems(2).Text.ToString)

        If newCD.Length > 0 And newCD IsNot "" Then

            theCD.SubItems(2).Text = newCD.ToString

        End If

    End Sub

    Private Sub txtInterval_LostFocus(sender As Object, e As System.EventArgs) Handles txtInterval.LostFocus

        If txtInterval.Text > 0 And (txtInterval.Text * 60000) <= 399999999 Then

            tmrRandom.Stop()
            tmrRandom.Interval = txtInterval.Text * 60000
            tmrRandom.Start()

        Else

            MsgBox("Interval is too long!")
            txtInterval.Focus()

        End If

    End Sub

    Private Sub txtInterval_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtInterval.TextChanged

    End Sub
End Class